﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace HenIT.BizTalk.Report
{
    public partial class ReportViewer : Form
    {
        #region Properties
        private Color heading1Color = Color.Maroon;
        public Color Heading1Color
        {
            get { return heading1Color; }
            set { heading1Color = value; }
        }
        private Color heading2Color = Color.Blue;
        public Color Heading2Color
        {
            get { return heading2Color; }
            set { heading2Color = value; }
        }
        private Color heading3Color = Color.DarkBlue;
        public Color Heading3Color
        {
            get { return heading3Color; }
            set { heading3Color = value; }
        }
        private Color warningColor = Color.Red;
        public Color WarningColor
        {
            get { return warningColor; }
            set { warningColor = value; }
        } 
        #endregion

        public ReportViewer()
        {
            InitializeComponent();
        }

        #region Context menu events
        private void copyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            rtxtReport.Copy();
        }

        private void selectAllToolStripMenuItem_Click(object sender, EventArgs e)
        {
            rtxtReport.SelectAll();
        }

        private void saveOutputToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                rtxtReport.SaveFile(saveFileDialog1.FileName);
            }
        } 

        private void wrapTextToolStripMenuItem_Click(object sender, EventArgs e)
        {
            rtxtReport.WordWrap = wrapTextToolStripMenuItem.Checked;
        }
        #endregion

        public void ClearOutput()
        {
            rtxtReport.Clear();
        }

        #region RTF writing
        public void WriteHeading(string text)
        {
            AddFormatRTFLine(text, FontStyle.Bold, 14, heading1Color);
        }
        public void WriteHeading2(string text)
        {
            AddFormatRTFLine(text, FontStyle.Bold, 12, heading2Color);
        }
        public void WriteHeading2(int indentation, string text)
        {
            AddFormatRTFLine(new string('\t', indentation) + text, FontStyle.Bold, 12, heading2Color);
        }
        public void WriteHeading3(string text)
        {
            AddFormatRTFLine(text, FontStyle.Regular, 10, heading3Color);
        }
        public void WriteHeading3(int indentation, string text)
        {
            AddFormatRTFLine(new string('\t', indentation) + text, FontStyle.Regular, 10, heading3Color);
        }
        public void WriteLine(string text)
        {
            AddFormatRTFLine(text, FontStyle.Regular, 8, SystemColors.WindowText);
        }
        public void WriteLine(int indentation, string text)
        {
            AddFormatRTFLine(new string('\t', indentation) + text, FontStyle.Regular, 8, SystemColors.WindowText);
        }
        public void WriteLineWarning(string text)
        {
            AddFormatRTFLine(text, FontStyle.Regular, 8, warningColor);
        }
        public void WriteLineWarning(int indentation, string text)
        {
            if (text.Contains("\n"))
            {
                text = text.Replace("\n", "\n" + new string('\t', indentation));
            }
            AddFormatRTFLine(new string('\t', indentation) + text, FontStyle.Regular, 8, warningColor);
        }
        public void WriteLine(string text, Color textColor)
        {
            AddFormatRTFLine(text, FontStyle.Regular, 8, textColor);
        }
        private void AddFormatRTFLine(string text, FontStyle fs, float fontsize, Color textColor)
        {
            if (text.Length > 0)
            {
                int startpos = rtxtReport.TextLength;
                rtxtReport.AppendText(text);
                int endpos = rtxtReport.TextLength;

                rtxtReport.SelectionStart = startpos; // rtxtReport.Text.Length - text.Length;
                rtxtReport.SelectionLength = endpos - startpos;// text.Length;
                rtxtReport.SelectionFont = new Font("Microsoft Sans Serif", fontsize, fs);
                rtxtReport.SelectionColor = textColor;
            }
            rtxtReport.AppendText("\r\n");
        }
        #endregion

        private void ReportViewer_Load(object sender, EventArgs e)
        {
            rtxtReport.SelectionStart = 0;
            rtxtReport.ScrollToCaret();
        }


    }
}
